package SocketClient;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class Client {
    private int port;
    private String host;
    Socket client=null;
    OutputStream os =null;
    InputStream is =null;
    byte data[]=new byte[128];
    public Client(String host,int port){
        this.host=host;
        this.port=port;
    }

    public boolean Connect(){
        try{
            this.client=new Socket(host,port);
            if(this.client==null){
                return false;
            }
            return true;
        }catch (IOException e){
            e.printStackTrace();
            return false;
        }
    }

    public  boolean Operation(String Command) throws IOException {
        if(client==null ){
            return false;
        }
        os= client.getOutputStream();
        is= client.getInputStream();
        os.write(Command.getBytes(StandardCharsets.UTF_8));
        int size = is.read(data);
        if(new String(data,0,size).equals("OK")){
            return true;
        }else{
            return false;
        }
    }


    public String OptionReadCard() throws IOException {
        if(client==null ){
            return null;
        }
        os= client.getOutputStream();
        is= client.getInputStream();
        os.write("readCard".getBytes(StandardCharsets.UTF_8));
        int size = is.read(data,0,14);
        String result=new String(data,0,2);
        if(result.contains("OK")){
            String substring=new String(data,2,12);
            return substring;
        }else{
            return null;
        }

    }
    public boolean makeCard(int idSequence) throws IOException{
        if(client==null ){
            return false;
        }
        os= client.getOutputStream();
        is= client.getInputStream();
        StringBuilder builder=new StringBuilder();
        builder.append("makeCard");
        byte []UserId=new byte[5];
        UserId[0]= (byte) (idSequence/10000);
        UserId[1]=(byte)(idSequence/1000%10);
        UserId[2]=(byte)(idSequence/100%10);
        UserId[3]=(byte)(idSequence/10%10);
        UserId[4]=(byte)(idSequence%10);
        builder.append(String.valueOf(UserId[0]));
        builder.append(String.valueOf(UserId[1]));
        builder.append(String.valueOf(UserId[2]));
        builder.append(String.valueOf(UserId[3]));
        builder.append(String.valueOf(UserId[4]));
        os.write(builder.toString().getBytes(StandardCharsets.UTF_8));
        int size = is.read(data);
        String result=new String(data,0,size);
        if(result.contains("OK")){
            return true;
        }else{
            return false;
        }
    }


    public boolean disConnect() throws IOException {
        if(client==null ){
            return false;
        }
        os= client.getOutputStream();
        is= client.getInputStream();
        os.write("bye".getBytes(StandardCharsets.UTF_8));
        if(client!=null && is!=null &&os!=null){
            os.close();
            is.close();
            client.close();
            return true;
        }
        return false;
    }
}
